//
//  AgriculturalMaterialsMallViewController.m
//  AnXin100
//
//  Created by 王合润 on 10/19/18.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AgriculturalMaterialsMallViewController.h"
#import "AgriculturalMaterialsMallCollectionViewCell.h"
//#import "AgriculturalMaterialsClassifyViewController.h"
//#import "ServiceViewController.h"


@interface AgriculturalMaterialsMallViewController ()<SDCycleScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) SDCycleScrollView *headImageView;

@property (nonatomic, strong) UIView *menuView;

@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, strong) UICollectionView *mainCollectionView;
@end

@implementation AgriculturalMaterialsMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainCollectionView];
    [PublicMonMethods settitle:@"农业商城" viewController:self];
    [PublicMonMethods setupNavigationItems:self];
}


- (UIView *)tableHeaderView {
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, -250, SCREEN_WIDTH, 250)];
        [_tableHeaderView addSubview:self.headImageView];
        [_tableHeaderView addSubview:self.menuView];
        
    }
    return _tableHeaderView;
}

- (SDCycleScrollView *)headImageView {
    if (!_headImageView) {
        NSArray *imageNames = @[@"m1.jpeg",
                                @"m2.jpeg",
                                @"m3.jpeg",
                                @"m4.jpeg",
                                @"m5.jpeg" // 本地图片请填写全名
                                ];
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
        _headImageView.delegate = self;
        _headImageView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _headImageView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _headImageView;
}


- (UIView *)menuView {
    if (!_menuView) {
        NSArray *dataAry = @[@{@"title" : @"农资分类",
                               @"icon" : @"classify"},
                             @{@"title" : @"农资推荐",
                               @"icon" : @"recommend"},
                             @{@"title" : @"服务网点",
                               @"icon" : @"network"},
                             @{@"title" : @"药效试验",
                               @"icon" : @"experiment"},
                             @{@"title" : @"排行榜",
                               @"icon" : @"ranking"}];
        _menuView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headImageView.frame), SCREEN_WIDTH, 90)];
        for (NSInteger i = 0; i < dataAry.count; i++) {
            NSDictionary *dic = dataAry[i];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = 100 + i;
            btn.frame = CGRectMake(SCREEN_WIDTH / 5 * i, 0, SCREEN_WIDTH / 5, 90);
            [btn setTitle:dic[@"title"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            [btn setImage:[UIImage imageNamed:dic[@"icon"]] forState:UIControlStateNormal];
            [btn setButtonLayout:CHButtonLayoutTitleBottom offset:0];
            [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
            [_menuView addSubview:btn];
        }
    }
    return _menuView;
}

- (UICollectionView *)mainCollectionView {
    if (!_mainCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/ 2, 178);
        
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        [_mainCollectionView registerClass:[AgriculturalMaterialsMallCollectionViewCell class] forCellWithReuseIdentifier:@"AgriculturalMaterialsMallCollectionViewCell"];
        _mainCollectionView.contentInset = UIEdgeInsetsMake(250, 0, 0, 0);
        _mainCollectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _mainCollectionView.backgroundColor = [UIColor whiteColor];
        [_mainCollectionView addSubview:self.tableHeaderView];
        
    }
    return _mainCollectionView;
}


#pragma mark - UICollectionView Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AgriculturalMaterialsMallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AgriculturalMaterialsMallCollectionViewCell" forIndexPath:indexPath];
    cell.dic = @{@"title" : @"云南夏黑葡萄 新鲜无籽提子水果",
                 @"price" : @"￥12/kg"};
    
    return cell;
}


- (void)btnClickAction:(UIButton *)sender {
    if (sender.tag == 100) {
//        AgriculturalMaterialsClassifyViewController *vc = [[AgriculturalMaterialsClassifyViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    if (sender.tag == 102) {
//        ServiceViewController *vc = [[ServiceViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - Action
- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}



@end
